package com.javasm.controller;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.PageInfo;
import com.javasm.entity.ReturnCodeEnum;
import com.javasm.entity.ReturnResult;
import com.javasm.entity.RoomCategory;
import com.javasm.service.RoomCategoryService;
import com.javasm.service.impl.RoomCategoryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/playRoomCategory/*")
public class RoomCategoryServlet extends BaseServlet {

    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");
        Integer page = 1;
        if (pageStr != null && !"".equals(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        Integer pageSize = 5;
        if (pageSizeStr != null && !"".equals(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        String categoryName = req.getParameter("categoryName");
        String parentIdStr = req.getParameter("parentId");
        Integer parentId = null;
        if (parentIdStr != null && !"".equals(parentIdStr)) {
            parentId = Integer.parseInt(parentIdStr);
        }
        RoomCategory inputCategory = new RoomCategory(categoryName, parentId);
        RoomCategoryService roomCategoryService = new RoomCategoryServiceImpl();
        Integer total = roomCategoryService.countCategory(inputCategory);
        //总页数计算公式 total%pageSize 能不能整除
        //能整除    total/pageSize   页
        //不能整除  total/pageSize+1  页
        Integer totalPage ;
        totalPage = total%pageSize==0?total/pageSize:total/pageSize+1;
        //检查当前要查的页数 是否超过了最大页数
        //如果超过了 则用最后一页查
        //修复bug时 有可能产生新bug
        //功能完成后 需要重新测试 之前测通的功能 可能会与现在的功能产生冲突 造成bug
        if(page!=1){
            if(page>totalPage){
                page = totalPage;
            }
        }
        List<RoomCategory> playRoomCategories = roomCategoryService.listAllCategory(inputCategory, page, pageSize);
        PageInfo returnPageInfo = new PageInfo(page,pageSize,total);
        //3反馈操作结果
        ReturnResult rrs = new ReturnResult();
        if(playRoomCategories.size()>0){
            rrs.setCode(ReturnCodeEnum.QUERY_SUCCESS.getCode());
            rrs.setMsg(ReturnCodeEnum.QUERY_SUCCESS.getMsg());

            rrs.setPageInfo(returnPageInfo);
            rrs.setReturnData(playRoomCategories);
        }else{
            rrs.setCode(ReturnCodeEnum.QUERY_NODATA.getCode());
            rrs.setMsg(ReturnCodeEnum.QUERY_NODATA.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();


    }
    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String categoryIdStr = req.getParameter("categoryId");
        String categoryName = req.getParameter("categoryName");
        String parentIdStr = req.getParameter("parentId");
        Integer parentId = null;
        Integer categoryId = null;
        if (parentIdStr != null && !"".equals(parentIdStr)) {
            parentId = Integer.parseInt(parentIdStr);
        }
        if(categoryIdStr!=null&&!"".equals(categoryIdStr)){
            categoryId = Integer.parseInt(categoryIdStr);
        }
        RoomCategory inputCategory = new RoomCategory(categoryId,categoryName,null, parentId);
        RoomCategoryService roomCategoryService = new RoomCategoryServiceImpl();
        Integer resNum = roomCategoryService.addCategory(inputCategory);
        ReturnResult rrs = null;
        if(resNum>0){
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String categoryIdStr = req.getParameter("categoryId");
        Integer categoryId = Integer.parseInt(categoryIdStr);
        RoomCategoryService roomCategoryService = new RoomCategoryServiceImpl();
        Integer resNum = roomCategoryService.deleteCategoryById(categoryId);
        ReturnResult rrs = null;
        if(resNum>0){
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }
    public void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String categoryIdStr = req.getParameter("categoryId");
        String categoryName = req.getParameter("categoryName");
        String parentIdStr = req.getParameter("parentId");
        Integer categoryId = null;
        Integer parentId = null;
        if (parentIdStr != null && !"".equals(parentIdStr)) {
            parentId = Integer.parseInt(parentIdStr);
        }
        if (categoryIdStr != null && !"".equals(categoryIdStr)) {
            categoryId = Integer.parseInt(categoryIdStr);
        }
        RoomCategory inputCategory = new RoomCategory(categoryId, categoryName,null, parentId);
        RoomCategoryService roomCategoryService = new RoomCategoryServiceImpl();
        Integer resNum = roomCategoryService.editCategory(inputCategory);
        ReturnResult rrs = null;
        if(resNum>0){
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }
    public void getCategoryById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String categoryIdStr = req.getParameter("categoryId");
        Integer categoryId = Integer.parseInt(categoryIdStr);
        RoomCategoryService roomCategoryService = new RoomCategoryServiceImpl();
        RoomCategory roomCategory = roomCategoryService.getCategoryById(categoryId);
        ReturnResult rrs = null;
        if(roomCategory !=null){
            rrs = new ReturnResult(ReturnCodeEnum.QUERY_SUCCESS.getCode(),
                    ReturnCodeEnum.QUERY_SUCCESS.getMsg(), roomCategory);
        }else {
            rrs = new ReturnResult(ReturnCodeEnum.QUERY_NODATA.getCode(),
                    ReturnCodeEnum.QUERY_NODATA.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }
    public void listCategoryForSelect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RoomCategoryService roomCategoryService = new RoomCategoryServiceImpl();
        List<RoomCategory> list = roomCategoryService.listCategoryForSelect(0);
        ReturnResult rrs = null;
        if(list!=null&&list.size()>0){
            rrs = new ReturnResult(ReturnCodeEnum.QUERY_SUCCESS.getCode(),
                    ReturnCodeEnum.QUERY_SUCCESS.getMsg(),list);
        }else {
            rrs = new ReturnResult(ReturnCodeEnum.QUERY_NODATA.getCode(),
                    ReturnCodeEnum.QUERY_NODATA.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }
}
